package com.example.test.teacher;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test.R;
import com.example.test.student.EditLesson;
import com.example.test.utils.Http;

import java.util.LinkedHashMap;

public class SetChen extends AppCompatActivity {
    private EditText name;
    private EditText grade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_chen);
        this.init();
    }
    public void init(){
        this.name = findViewById(R.id.chen_name);
        this.grade = findViewById(R.id.chen_value);
    }
    public void edit(View view){
        String Url = this.dataUpdate(); // 处理url
        if (Url.equals("false")) {
            Toast.makeText(SetChen.this, "不能为空", Toast.LENGTH_LONG).show();
        } else {
            createLesson(Url);
        }
    }

    public void createLesson(final String url){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String res = Http.sendGet(url);
                final LinkedHashMap<String, Object> hashMaps = Http.stringToJsonObject(res);
//                Log.d("HttpGET", (String) hashMaps.get("msg"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        assert hashMaps != null;
                        Toast.makeText(SetChen.this, (String) hashMaps.get("msg"), Toast.LENGTH_LONG).show();
                        if ((int) hashMaps.get("code") == 1) {
                            canBack();
                        } else {
                            Toast.makeText(SetChen.this, "创建失败", Toast.LENGTH_LONG).show();

                        }
                    }
                });
            }
        }).start();
    }
    public String dataUpdate() {
        String[] data = {name.getText().toString(), grade.getText().toString()};
        String[] dataName = {"name", "garle"};
        StringBuilder url;
        url = new StringBuilder("http://36428pn632.qicp.vip/student/index.php/course/course/update/");
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals("")) {
                return "false";
            }
            if (i == 0) {
                url.append("?").append(dataName[i]).append("=").append(data[i]);
            } else {
                url.append("&").append(dataName[i]).append("=").append(data[i]);
            }
        }
        return url.toString();
    }

    public void canBack() {
        Intent intent = new Intent(SetChen.this, com.example.test.student.MainActivity.class);
        //启动
        startActivity(intent);
    }
}
